package com.zzh.base.utils

import android.util.Log
import com.google.gson.internal.`$Gson$Types`.getRawType
import com.hammera.common.baseUI.BasePresenter
import com.zzh.base.BaseModel
import java.lang.reflect.ParameterizedType
object TUtils {
    fun <T> getT(o: Any, i: Int): T? {

        try {
            if (o.javaClass.genericSuperclass is ParameterizedType) {
                val type = o.javaClass.genericSuperclass as ParameterizedType
                var types = type.actualTypeArguments
                if(types[i]==java.lang.Object::javaClass)
                    Log.e("TUtils", o.javaClass.name+"getT $i " + getRawType(types[i]))
                var obj = (types[i] as Class<T>).newInstance()
                return if (obj is BaseModel<*> || obj is BasePresenter<*, *>) obj else null
            } else if (o.javaClass.genericSuperclass is Class<*>) {
                val type = o.javaClass.superclass?.genericSuperclass as ParameterizedType
                val types = type.actualTypeArguments
                var obj = (types[i] as Class<T>).newInstance()
                return if (obj is BaseModel<*> || obj is BasePresenter<*, *>) obj else null
            }
        } catch (e: Throwable) {
            e.printStackTrace()
            Log.e("TUtils", o.javaClass.name)
        }

        return null
    }


}